package com.lagou.edu.controller;

import com.lagou.edu.pojo.AuthCode;
import com.lagou.edu.service.AuthCodeService;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/api/code/auth")
@RefreshScope
public class AuthCodeController {

    @Value("${lagou.message}")
    private String message;
    @Autowired
    private AuthCodeService authCodeService;

    @RequestMapping("/config")
    public String viewconfig() {
        return "lagou message ==>" + message;
    }

    /**
     * 获取验证码，并保存至数据库
     *
     * @param code
     * @return
     */
    @RequestMapping("/save")
    public ModelMap save(@RequestBody AuthCode code) {
        ModelMap modelMap = new ModelMap();
        //为空则返回错误
        if (StringUtils.isEmpty(code.getEmail())) {
            modelMap.addAttribute("code", 404);
            modelMap.addAttribute("message", "参数错误");
            return modelMap;
        }
        //将对应的邮箱、验证码。时间等参数保存至数据库
        Long id = authCodeService.save(code);
        if (id > 0) {
            //成功就发送
            modelMap.addAttribute("code", 200);
            modelMap.addAttribute("message", "发送成功");
        } else {
            //这里的失败就是十分钟内多次获取的失败
            modelMap.addAttribute("code", 304);
            modelMap.addAttribute("message", "十分钟内只允许获取⼀次验证码");
        }
        return modelMap;
    }

    /**
     * 验证验证码是否匹配
     *
     * @param authCode
     * @return
     */
    @RequestMapping("/get")
    public ModelMap get(@RequestBody AuthCode authCode) {
        ModelMap modelMap = new ModelMap();
        System.out.println(message);
        //参数错误，邮箱和验证码必传
        if (StringUtils.isEmpty(authCode.getEmail()) || StringUtils.isEmpty(authCode.getCode())) {
            modelMap.addAttribute("code", 404);
            modelMap.addAttribute("message", "参数错误");
            return modelMap;
        }
        //获取验证码
        AuthCode unique = authCodeService.getUnique(authCode);
        if (unique != null) {
            Seconds seconds = Seconds.secondsBetween(new DateTime(unique.getExpireTime()), new DateTime(new Date()));
            //计算时差
            if (seconds.getSeconds() <= 0) {
                //匹配上了，
                modelMap.addAttribute("code", 200);
                modelMap.addAttribute("message", "验证码匹配");
                modelMap.addAttribute("data", true);
            } else {
                //超时了
                modelMap.addAttribute("code", 304);
                modelMap.addAttribute("message", "验证码已过期");
                modelMap.addAttribute("data", false);
            }
        } else {
            //没匹配上
            modelMap.addAttribute("code", 304);
            modelMap.addAttribute("message", "验证码不匹配");
            modelMap.addAttribute("data", false);
        }
        return modelMap;
    }

}
